using System;
using System.Collections.Generic;
using System.Text;

namespace Pyramid
{
    public class Statistics
    {
       
        public double[] FindMean(List<double[]> myData)
        {
            int i,j;
            double[] m=new double[myData[0].Length];
            for (i = 0; i < m.Length; i++)
            {
                m[i] = new double();
                m[i] = 0;
            }
           
            for (i = 0; i < myData.Count; i++)
                for (j=0;j<myData[i].Length;j++)
                    m[j] += myData[i][j];
            for (i = 0; i < m.Length; i++)
                m[i] = m[i] / myData.Count;
            return m;
        }

        public double[] FindVariance(List<double[]> myData)
        {
            int i, j;
            double[] m = new double[myData[0].Length];
            double[] s = new double[myData[0].Length];
            for (i = 0; i < m.Length; i++)
            {
                m[i] = new double();
                m[i] = 0;
                s[i] = new double();
                s[i] = 0;
            }
           
            for (i = 0; i < myData.Count; i++)
                for (j = 0; j < myData[i].Length; j++)
                {
                    m[j] += myData[i][j];
                    s[j] += Math.Pow(myData[i][j], 2);
                }
            for (i = 0; i < m.Length; i++)
            {
                m[i] = m[i] / myData.Count;
                s[i] = s[i] / myData.Count;
            }
            for (i = 0; i < m.Length; i++)
            {
                m[i] = Math.Sqrt(s[i]- Math.Pow(m[i],2));
                
            }

            return m;
        }

        public double[] FindCovariance(List<double[]> myData1, List<double[]> myData2)
        {
            int i, j;
            double[] m1 = FindMean(myData1);
            double[] m2 = FindMean(myData2);
            double[] c = new double[myData1[0].Length];
            for (i = 0; i < c.Length; i++)
            {
                c[i] = new double();
                c[i] = 0;
            }

            for (i = 0; i < myData1.Count; i++)
                if (i < myData2.Count)
                {
                    for (j = 0; j < myData1[i].Length; j++)
                    {
                        if (j < myData2[i].Length)
                            c[j] += (myData1[i][j] - m1[j]) * (myData2[i][j] - m2[j]);
                    }
                }
            for (i = 0; i < c.Length; i++)
            {
                c[i] = c[i] / myData1.Count;
                
            }
            return c;
        }
    }
}
